﻿using System;
using System.Collections.Generic;
using imeem.api;
using imeem.library;

namespace imeem.services
{
    public class PlaylistService : imeemService
    {
        #region GetById

        public List<PlaylistData> GetById(params string[] playlistIds)
        {
            if (playlistIds == null || playlistIds.Length == 0)
            {
                throw new Exception("At least 1 playlist Id is required");
            }

            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("playlistIds", Join<string>(playlistIds));

            imeemResult result = base.MakeRequest(imeem.dal.methods.Playlist.PlaylistsGetInfo, parameters);

            List<PlaylistData> playlists = new List<PlaylistData>();
            foreach (Item item in result.Items)
            {
                PlaylistData playlist = item as PlaylistData;
                if (playlist != null)
                {
                    playlists.Add(playlist);
                }
            }
            return playlists;
        }

        #endregion

        #region Search

        public List<PlaylistData> Search(string query, PlaylistType playlistType)
        {
            return Search(query, playlistType, null, null);
        }

        public List<PlaylistData> Search(string query, PlaylistType playlistType, int? offset, int? numResults)
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("query", query);
            parameters.Add("playlistType", Enum.GetName(typeof(PlaylistType), playlistType));
            
            if (offset.HasValue)
            {
                parameters.Add("offset", offset.Value.ToString());
            }
            if (numResults.HasValue)
            {
                parameters.Add("numResults", numResults.Value.ToString());
            }

            imeemResult result = base.MakeRequest(imeem.dal.methods.Playlist.PlaylistsSearch, parameters);

            List<PlaylistData> playlists = new List<PlaylistData>();
            foreach (Item item in result.Items)
            {
                PlaylistData playlist = item as PlaylistData;
                if (playlist != null)
                {
                    playlists.Add(playlist);
                }
            }
            return playlists;
        }

        #endregion

        #region GetByUser

        public List<PlaylistData> GetByUser(UserData user, PlaylistType playlistTypes)
        {
            return GetByUser(user.Id, playlistTypes, null);
        }

        public List<PlaylistData> GetByUser(UserData user, PlaylistType playlistTypes, MediaFilter? mediaFilter)
        {
            return GetByUser(user.Id, playlistTypes, mediaFilter);
        }

        public List<PlaylistData> GetByUser(string userId, PlaylistType playlistTypes)
        {
            return GetByUser(userId, playlistTypes, null);
        }

        public List<PlaylistData> GetByUser(string userId, PlaylistType playlistTypes, MediaFilter? mediaFilter)
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("userId", userId);
            parameters.Add("playlistTypes", joinPlaylistTypes(playlistTypes));
            
            if (mediaFilter.HasValue)
            {
                parameters.Add("playlistType", joinPlaylistTypes(playlistTypes));
            }

            imeemResult result = base.MakeRequest(imeem.dal.methods.Playlist.PlaylistsGetByUser, parameters);

            List<PlaylistData> playlists = new List<PlaylistData>();
            foreach (Item item in result.Items)
            {
                PlaylistData playlist = item as PlaylistData;
                if (playlist != null)
                {
                    playlists.Add(playlist);
                }
            }
            return playlists;
        }

        #endregion

        #region private - joinPlaylistTypes

        private string joinPlaylistTypes(PlaylistType playlistTypes)
        {
            List<string> playlistTypeList = new List<string>();

            if ((playlistTypes & PlaylistType.musicPlaylist) == PlaylistType.musicPlaylist)
            {
                playlistTypeList.Add("musicPlaylist");
            }

            if ((playlistTypes & PlaylistType.videoPlaylist) == PlaylistType.videoPlaylist)
            {
                playlistTypeList.Add("videoPlaylist");
            }

            if ((playlistTypes & PlaylistType.photoPlaylist) == PlaylistType.photoPlaylist)
            {
                playlistTypeList.Add("photoPlaylist");
            }

            return Join<string>(playlistTypeList);
        }

        #endregion
    }
}
